﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

namespace Thor.Common
{
    public class TimeZone
    {
        [Key]
        public string Name { get; set; }

        public DateTime Now
        {
            get
            {
                var currentUtcTime = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified);
                var currentLocalTime = TimeZoneInfo.ConvertTimeFromUtc(currentUtcTime, GetTimeZoneInfo());
                return currentLocalTime;
            }
        }

        private TimeZoneInfo GetTimeZoneInfo()
        {
            if (string.IsNullOrEmpty(Name))
            {
                return TimeZoneInfo.Utc;
            }
            else
            {
                var timeZoneInfo = (from t in TimeZoneInfo.GetSystemTimeZones()
                                    where t.Id == Name
                                    select t).FirstOrDefault();

                if (timeZoneInfo == null)
                    return TimeZoneInfo.Utc;
                else
                    return timeZoneInfo;
            }
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
